<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>20题求n的阶乘</title>
    <script>
        function caculateSum(flag) {
            var num = prompt(flag ? '请输入正确的正整数' : '请输入一个正整数');
            if (!/^[0-9]*$/i.test(num)) {
                caculateSum(true);
                return;
            }
            
            let total = 1;
            let len = parseInt(num);
            for (let i = len; i >=1; i--) {
                total *= i;
            }
            let sum = document.getElementById('sum');
            sum.innerHTML = num + '的阶乘为:' + total;
        }
    </script>
</head>
<body>
    <p>
        点击按钮开始输入，并计算阶乘<input type="button" value="点击输入" onclick="caculateSum()">
    </p>
    <p id="sum">n的阶乘：</p>
</body>
</html>